import cv2
import numpy as np

# 读取图像和模板
temp = './img/Fight-bingbaofashengqi.png'
screen = './cache/screenCap.png'
image = cv2.imread(screen)
template = cv2.imread(temp)

# 将图像转换为灰度图，因为模板匹配通常在灰度图上进行
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray_template = cv2.cvtColor(template, cv2.COLOR_BGR2GRAY)

# 使用cv2.matchTemplate()方法找到模板在图像中的位置
result = cv2.matchTemplate(gray_image, gray_template, cv2.TM_CCOEFF_NORMED)

# 设置一个阈值，例如0.8
threshold = 0.9
locations = np.where(result >= threshold)
locations = list(zip(locations[1], locations[0]))  # 转换成(x, y)格式

# 过滤并选择X坐标最小的位置
if locations:
    min_x_location = min(locations, key=lambda loc: loc[0])  # 选择X坐标最小的位置
    print("X坐标最小的位置:", min_x_location)
else:
    print("没有找到符合阈值的匹配。")